package kr.co.happenstance.lib;

import java.io.*;

import com.google.gson.Gson;
import com.google.gson.internal.Primitives;

import kr.co.happenstance.http.HttpRequestor;
import android.util.*;

public class CommonLib {
	protected final String TAG = "<<<CommonLib>>>";
	
	protected HttpRequestor httpRequestor;
	protected InputStream inputStream;
	protected BufferedReader br;
	protected StringBuffer buffer;
	
	
	/**
	 * 
	 * @param classOfT
	 * @return
	 * @throws Exception
	 */
	protected <T> T send(Class<T> classOfT) throws Exception{
		Object obj = null;;
		try{
			inputStream = httpRequestor.sendPost();
			Log.i(TAG, "=================================================================");
			Log.i(TAG, "Request URL["+ this.httpRequestor.requestFullUrl()+"]");
			Log.i(TAG, "Parameters ["+ this.httpRequestor.getParameters()+"]");
			Log.i(TAG, "Response Code["+ this.httpRequestor.getResponseCode()+"]");
			Log.i(TAG, "Response Message["+ this.httpRequestor.getResponseMessage()+"]");
			
			if(inputStream != null){
				br = new BufferedReader(new InputStreamReader(inputStream));
				obj = new Gson().fromJson(br, classOfT);
			}			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return (obj == null)? null: Primitives.wrap(classOfT).cast(obj); 
	}
	

	
}
